package com.eco.netty;

import com.eco.utils.DateAndHexUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/* loaded from: newfishway-1.0.0-SNAPSHOT.jar:BOOT-INF/classes/com/eco/netty/Sensor6Decoder.class */
public class Sensor6Decoder extends ByteToMessageDecoder {
    private static final Logger log = LoggerFactory.getLogger((Class<?>) Sensor6Decoder.class);
    public static int BASE_LENTH = 13;
    private String hdsw1;

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
        try {
            if (in.readableBytes() > 2048) {
                in.skipBytes(in.readableBytes());
            }
            String result = byteBuf2String(in);
            if (result == null) {
                return;
            }
            out.add(result);
        } catch (Exception e) {
            log.error("自定义解码器异常:{}", e.toString());
        }
    }

    private String byteBuf2String(ByteBuf byteBuf) throws Exception {
        int endReader = byteBuf.readableBytes();
        byte[] useless = new byte[2];
        byte[] para = new byte[4];
        byteBuf.readBytes(useless);
        byteBuf.readByte();
        byteBuf.readBytes(para);
        String hdsw = DateAndHexUtil.byte2FloatStr2(para);
        if (NumberUtils.isCreatable(hdsw) && Float.parseFloat(hdsw) >= 0.0f && Float.parseFloat(hdsw) < 10.0f) {
            this.hdsw1 = hdsw;
        } else {
            log.error("hdsw值异常:{}", hdsw);
            if (this.hdsw1 != null) {
                hdsw = this.hdsw1;
            } else {
                hdsw = "0.01";
            }
        }
        byteBuf.readBytes(para);
        byteBuf.readBytes(useless);
        byteBuf.readerIndex(endReader);
        return hdsw;
    }
}